package com.lanxin.hr.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanxin.hr.entity.ZzAdjustment;
import com.lanxin.hr.mapper.ZzAdjustmentMapper;
import com.lanxin.hr.service.IZzAdjustmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanxin.hr.service.IZzEmployeeService;
import com.lanxin.hr.vo.AdjustmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2025-07-30
 */
@Service
public class ZzAdjustmentServiceImpl extends ServiceImpl<ZzAdjustmentMapper, ZzAdjustment> implements IZzAdjustmentService {
    @Autowired
    private ZzAdjustmentMapper adjustmentMapper;

    @Autowired
    private IZzEmployeeService employeeService;

    //生成岗位调动申请,同时修改员工的部门id和职位id
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public int addAdjustment(ZzAdjustment zzAdjustment) {
        //生成调岗申请
        if(adjustmentMapper.insert(zzAdjustment)==0){
            throw new RuntimeException("生成调岗申请失败");
        }
        //改变员工的部门和职位
        if(employeeService.adjustEmp(zzAdjustment)==0){
            throw new RuntimeException("调动岗位失败");
        }
        return 1;
    }

    //分页获取员工调动详情
    @Override
    public IPage<AdjustmentVo> pageAdjustmentVo(Integer page, Integer pageSize,Integer empId,String startDate,String endDate) {
        if(page==null||page<1){
            page=1;
        }
        if(pageSize==null||pageSize<5){
            pageSize=5;
        }
        Page<AdjustmentVo> adjustmentVoPage = new Page<>(page, pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("empId",empId);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        if(startDate!=null&&!startDate.equals("")){
            //atStartOfDay());时间部分补 00:00:00
            map.put("startDate", LocalDate.parse(startDate).atStartOfDay().format(formatter));
        }else {
            map.put("startDate", null);
        }
        if(endDate!=null&&!endDate.equals("")){
            //atTime(23,59,59)包含当天最后一秒
            map.put("endDate", LocalDate.parse(endDate).atTime(23,59,59).format(formatter));
        }else {
            map.put("endDate",null);
        }
        IPage<AdjustmentVo> adjustmentVos = adjustmentMapper.pageAdjustmentVo(adjustmentVoPage,map);
        return adjustmentVos;
    }

    //分页获取单个员工的调动记录
    @Override
    public IPage<AdjustmentVo> pageAdjustmentVoByEmpId(Integer page, Integer pageSize, Integer empId, String startDate, String endDate) {
        if(page==null||page<1){
            page=1;
        }
        if(pageSize==null||pageSize<5){
            pageSize=5;
        }
        Page<AdjustmentVo> adjustmentVoPage = new Page<>(page, pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("empId",empId);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        if(startDate!=null&&!startDate.equals("")){
            //atStartOfDay());时间部分补 00:00:00
            map.put("startDate", LocalDate.parse(startDate).atStartOfDay().format(formatter));
        }else {
            map.put("startDate", null);
        }
        if(endDate!=null&&!endDate.equals("")){
            //atTime(23,59,59)包含当天最后一秒
            map.put("endDate", LocalDate.parse(endDate).atTime(23,59,59).format(formatter));
        }else {
            map.put("endDate",null);
        }
        IPage<AdjustmentVo> adjustmentVos = adjustmentMapper.pageAdjustmentVoBYEmpId(adjustmentVoPage,map);
        return adjustmentVos;
    }

}
